package org.jsola.hr.provider.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsola.admin.entity.SiteDO;
import org.jsola.cache.ICache;
import org.jsola.common.DateKit;
import org.jsola.common.IdGenerateKit;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.CacheKeyKit;
import org.jsola.hr.constant.ApprovalStatusEnum;
import org.jsola.hr.dao.ICompanyDAO;
import org.jsola.hr.dao.IEmpInfoDAO;
import org.jsola.hr.dto.EmpTicketDTO;
import org.jsola.hr.dto.PhoneVerificationCodeDTO;
import org.jsola.hr.dto.WeChatBindFromOffAccountDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.provider.IHrEmpInfoRelationService;
import org.jsola.hr.provider.IMessageProviderService;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.EmpAllInfoVO;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.hr.vo.EmpLoginResultVO;
import org.jsola.message.dto.ShortMessageDTO;
import org.jsola.message.service.ISmsSendService;
import org.jsola.user.constant.UserConstants;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserProperties;
import org.jsola.user.dto.JwtTokenDTO;
import org.jsola.user.entity.ThirdInfoDO;
import org.jsola.user.entity.UserDO;
import org.jsola.user.service.ICertificationService;
import org.jsola.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

import static org.jsola.hr.constant.HrConstants.EMP_TICKET_EXPIRE;
import static org.jsola.hr.constant.HrConstants.USER_CERTIFICATION;

/**
 * @author JanusMix
 */
@Slf4j
@Service("hrEmpInfoRelationService")
public class HrEmpInfoRelationServiceImpl implements IHrEmpInfoRelationService {

    @Autowired
    private IEmpInfoDAO empInfoDAO;

    @Autowired
    private IMessageProviderService messageProviderService;

    @Autowired
    private UserProperties userProperties;

    @Autowired
    private IEmpInfoService empInfoService;

    @Autowired
    private IUserProviderService userProviderService;

    @Autowired
    private ICertificationService iCertificationService;

    @Autowired
    private ISmsSendService sendShortMessage;

    @Autowired
    private ICache cache;

    @Autowired
    private ICompanyDAO companyDAO;

    @Autowired
    private IAdminProviderService adminProviderService;

    @Value("${spring.application.name}")
    private String appName;

    @Override
    public String genTicket(EmpTicketDTO empTicketDTO) {
        IdGenerateKit idGenerateKit = new IdGenerateKit(0, 0);
        String ticket = idGenerateKit.nextString();

        EmpInfoVO empInfoTmpVO = new EmpInfoVO();
        empInfoTmpVO.setUnionId(empTicketDTO.getUnionId());
        //ticket存入redis（过期时间：2小时）
        cache.set(CacheKeyKit.getTicketCacheKey(ticket), JSON.toJSONString(empInfoTmpVO));
        cache.expire(CacheKeyKit.getTicketCacheKey(ticket), EMP_TICKET_EXPIRE);
        return ticket;
    }

    @Override
    public Boolean hrSendShortMessage(String phone) {
        if (StringUtils.isBlank(phone)) {
            throw new ParamException("手机号不可为空");
        }
        //判断待入职员工是否存在
//        EmpInfoDO empInfoDO = this.checkEmpInfo(phone);
//        if (Objects.isNull(empInfoDO)) {
//            //判断手机号是否已存在用户信息
//            UserDO userDO = userProviderService.selectByPhone(phone);
//            if (Objects.isNull(userDO)) {
//                //用户不存在
////                throw new ParamException("待入职员工信息不存在");
//            }
//
//        }
        //发送短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phone);
        shortMessageDTO.setThirdId(this.userProperties.getMessage().getSmsTemplateId());
        shortMessageDTO.setScene("hr_emp_certification");

        return messageProviderService.sendShortMessage(shortMessageDTO, null);
    }

    @Override
    public Boolean hrSendShortMessage(String phone, String host) {
        if (StringUtils.isBlank(phone)) {
            throw new ParamException("手机号不可为空");
        }
        //发送短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phone);
        shortMessageDTO.setThirdId(this.userProperties.getMessage().getSmsTemplateId());
        shortMessageDTO.setScene(USER_CERTIFICATION);

        TokenUser tokenUser = iCertificationService.selectByPhoneAndDomain(phone, host);
        return messageProviderService.sendShortMessage(shortMessageDTO, tokenUser);
    }

    @Override
    public Boolean hrSendShortMessageByDeviceType(String phone, String deviceType) {
        if (StringUtils.isBlank(phone)) {
            throw new ParamException("手机号不可为空");
        }
        //发送短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phone);
        shortMessageDTO.setThirdId(this.userProperties.getMessage().getSmsTemplateId());
        shortMessageDTO.setScene(USER_CERTIFICATION);

        TokenUser tokenUser = iCertificationService.selectByPhoneAndDeviceType(phone, deviceType);
        return messageProviderService.sendShortMessage(shortMessageDTO, tokenUser);
    }

    @Override
    public EmpAllInfoVO verificationCodeLogin(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        if (StringUtils.isBlank(phoneVerificationCodeDTO.getPhone()) || StringUtils.isBlank(phoneVerificationCodeDTO.getCode())) {
            throw new ParamException("手机号和验证码不可为空");
        }
        //校验短信验证码
        boolean check = messageProviderService.verifyCaptcha("hr_emp_certification", phoneVerificationCodeDTO.getPhone(), phoneVerificationCodeDTO.getCode());
        if (!check) {
            throw new ParamException("验证码错误");
        }
        //判断员工是否存在
        EmpInfoDO empInfoDO = this.checkEmpInfo(phoneVerificationCodeDTO.getPhone());
        if (Objects.isNull(empInfoDO)) {
            EmpAllInfoVO empAllInfoVO = new EmpAllInfoVO();
            //判断手机号是否已存在用户信息
            UserDO userDO = userProviderService.selectByPhone(phoneVerificationCodeDTO.getPhone());
            if (Objects.isNull(userDO)) {
                //用户不存在
                empAllInfoVO.setResStatus(2);
            } else {
                //用户已存在
                empAllInfoVO.setResStatus(3);
//                ParamException.hasLength(empAllInfoTmpVO.getUnionId(), "微信已存在但微信unionId不存在");
//                UserLoginVO userLoginVO = userProviderService.bindWechatAndLogin(userDO.getId(), UserConstants.TO_B, empAllInfoTmpVO.getUnionId());
//                empAllInfoVO.setUserLoginVO(userLoginVO);
            }
            return empAllInfoVO;
        }
        if (empInfoDO.getStatus() == 1) {
            if ((!empInfoDO.getApprovalStatus().equals(ApprovalStatusEnum.APPRO_SUCCESS.getValue())) && (!empInfoDO.getApprovalStatus().equals(ApprovalStatusEnum.APPRO_DISABLE.getValue()))) {
                throw new ParamException("待入职员工暂未审批通过");
            }
        }

        //更新员工信息（更新unionId）
//        empInfoDO.setUnionId(empAllInfoTmpVO.getUnionId());
        empInfoDO.setGmtModified(DateKit.getToday());
        empInfoDAO.updateByPrimaryKey(empInfoDO);

        EmpAllInfoVO empAllInfoVO = empInfoService.findEmpInfoRelation(empInfoDO.getId(), empInfoDO.getSiteId());

        EmpInfoVO empInfoTmpVO = new EmpInfoVO();
        empInfoTmpVO.setId(empAllInfoVO.getId());
        empInfoTmpVO.setCompanyId(empAllInfoVO.getCompanyId());
        empInfoTmpVO.setSiteId(empAllInfoVO.getSiteId());
//        empInfoTmpVO.setUnionId(empAllInfoTmpVO.getUnionId());
        //ticket存入redis（过期时间：2小时）
//        cache.set(CacheKeyKit.getTicketCacheKey(phoneVerificationCodeDTO.getTicket()), JSON.toJSONString(empInfoTmpVO));
//        cache.expire(CacheKeyKit.getTicketCacheKey(phoneVerificationCodeDTO.getTicket()), EMP_TICKET_EXPIRE);
//        empAllInfoVO.setTicket(phoneVerificationCodeDTO.getTicket());
        if (empInfoDO.getStatus() == 1) {
            //待入职员工
            empAllInfoVO.setResStatus(1);
        } else {
            //已入职员工
            empAllInfoVO.setResStatus(4);
        }

        //判断手机号是否已存在用户信息（以前只在待入职员工信息不存在且存在用户时操作，现在统一开通绑定）
        UserDO userDO = userProviderService.selectByPhone(phoneVerificationCodeDTO.getPhone());
        if (!Objects.isNull(userDO)) {
//            UserLoginVO userLoginVO = userProviderService.bindWechatAndLogin(userDO.getId(), UserConstants.TO_B, empAllInfoTmpVO.getUnionId());
//            empAllInfoVO.setUserLoginVO(userLoginVO);
            empAllInfoVO.setUserDO(userDO);
        }
        return empAllInfoVO;
//        if (Objects.isNull(empInfoDO)) {
//            throw new ParamException("待入职员工信息不存在");
//        }
//        EmpAllInfoVO empAllInfoVO = empInfoService.findEmpInfoRelation(empInfoDO.getId(), empInfoDO.getSiteId());
//
//
//        IdGenerateKit idGenerateKit = new IdGenerateKit(0, 0);
//        String ticket = idGenerateKit.nextString();
//        //ticket存入redis（过期时间：2小时）
//        EmpInfoVO empInfoTmpVO = new EmpInfoVO();
//        empInfoTmpVO.setId(empAllInfoVO.getId());
//        empInfoTmpVO.setCompanyId(empAllInfoVO.getCompanyId());
//        empInfoTmpVO.setSiteId(empAllInfoVO.getSiteId());
//        //ticket存入redis（过期时间：2小时）
//        cache.set(CacheKeyKit.getTicketCacheKey(ticket), JSON.toJSONString(empInfoTmpVO));
//        cache.expire(CacheKeyKit.getTicketCacheKey(ticket), EMP_TICKET_EXPIRE);
//        empAllInfoVO.setTicket(ticket);
//
//        return empAllInfoVO;
    }

    @Override
    public EmpLoginResultVO verificationCodeLoginWeChat(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        EmpLoginResultVO empLoginResultVO = verificationCodeLoginWeChatForAdmin(phoneVerificationCodeDTO);
        UserLoginVO userLoginVO = empLoginResultVO.getUserLoginVO();
        Map<String, SiteDO> siteMap = adminProviderService.selectSiteMap();
        ParamException.notEmpty(siteMap, "未检测到租户信息，请刷新后重试");
        //判断员工是否存在
        Example example = new Example(EmpInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("userId", userLoginVO.getUserVO().getId())
                .andIn("siteId", siteMap.keySet())
                .andIn("status", Arrays.asList(2, 3));
        List<EmpInfoDO> empInfoDOList = empInfoDAO.selectByExample(example);

        //有在职员工
        if (!CollectionUtils.isEmpty(empInfoDOList)) {
            EmpInfoDO empInfoDO = null;
            //找到与登录租户相同的员工
            for (EmpInfoDO empInfo : empInfoDOList) {
                if (empInfo.getSiteId().equals(userLoginVO.getSiteId())) {
                    empInfoDO = empInfo;
                }
            }
            // 当前登录用户的租户下没有员工，则切换到有员工的租户下
            if (Objects.isNull(empInfoDO)) {
                empInfoDO = empInfoDOList.get(0);
                log.info("站点下不存在员工，切换租户到：" + empInfoDO.getSiteId());
                // 切换到当前员工用户的租户下，即登录到另一个站点
                JwtTokenDTO jwtTokenDTO = userProviderService.switchToken(userLoginVO.getJwtTokenDTO().getToken(), empInfoDO.getSiteId());
                userLoginVO.setJwtTokenDTO(jwtTokenDTO);
                // 更换返回的站点Id
                userLoginVO.setSiteId(empInfoDO.getSiteId());
                SiteDO siteDO = siteMap.get(empInfoDO.getSiteId());
                userLoginVO.setSiteName(siteDO.getName());
            }
            log.info("员工所在公司:" + empInfoDO.getCompanyId());
            CompanyDO companyDO = companyDAO.selectByPrimaryKey(empInfoDO.getCompanyId());
            if (empInfoDO.getUnionId() == null) {
                empInfoDO.setUnionId(userLoginVO.getUnionId());
                empInfoService.updateByIdSelective(empInfoDO, empInfoDO.getSiteId(), empInfoDO.getCreateUserId());
            }
            EmpAllInfoVO empAllInfoVO = empInfoDO.to(EmpAllInfoVO.class);
            empAllInfoVO.setCompanyName(companyDO.getName());
            empLoginResultVO.setEmpAllInfoVO(empAllInfoVO);
            //更新emp_info头像
            if (StringUtils.isNotBlank(empLoginResultVO.getHeadImgUrl())) {
                EmpInfoDO updatePhote = new EmpInfoDO();
                updatePhote.setId(empInfoDO.getId());
                updatePhote.setEmpPhoto(empLoginResultVO.getHeadImgUrl());
                empInfoService.updateByIdSelective(updatePhote);
            }
        }
        return empLoginResultVO;
    }

    @Override
    public EmpLoginResultVO verificationCodeLoginWeChatByDevicetype(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        EmpLoginResultVO empLoginResultVO = new EmpLoginResultVO();
        if (StringUtils.isBlank(phoneVerificationCodeDTO.getPhone()) || StringUtils.isBlank(phoneVerificationCodeDTO.getCode())) {
            throw new ParamException("手机号和验证码不可为空");
        }
        //校验短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phoneVerificationCodeDTO.getPhone());
        shortMessageDTO.setScene(USER_CERTIFICATION);
        shortMessageDTO.setCaptcha(phoneVerificationCodeDTO.getCode());
        shortMessageDTO.setAppName(appName);
        TokenUser tokenUser = iCertificationService.selectByPhoneAndDeviceType(phoneVerificationCodeDTO.getPhone(),
                phoneVerificationCodeDTO.getDeviceType());
        boolean check = sendShortMessage.verifyCaptcha(shortMessageDTO,tokenUser);
        if (!check) {
            throw new ParamException("验证码错误");
        }
        // 根据unionId获取第三方信息
        ThirdInfoDO thirdInfo = userProviderService.selectByUnionId(phoneVerificationCodeDTO.getUnionId());
        log.info("绑定手机号验证码thirdInfoDOTmp：{}", thirdInfo);
        if (Objects.nonNull(thirdInfo)) {
            empLoginResultVO.setHeadImgUrl(thirdInfo.getHeadImgUrl());
            empLoginResultVO.setNickName(thirdInfo.getNickName());
        }

        UserDO userDO = userProviderService.selectByPhone(phoneVerificationCodeDTO.getPhone());
        ParamException.notNull(userDO, "所填手机号用户不存在");
        //绑定微信并登录
        UserLoginVO userLoginVO = userProviderService.bindWechatAndLogin(userDO.getId(), UserConstants.TO_B, phoneVerificationCodeDTO.getUnionId());
        empLoginResultVO.setUserLoginVO(userLoginVO);

        //判断员工是否存在
        Example example = new Example(EmpInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("userId", userLoginVO.getUserVO().getId())
                .andIn("status", Arrays.asList(2, 3));
        List<EmpInfoDO> empInfoDOList = empInfoDAO.selectByExample(example);

        //有在职员工
        if (!CollectionUtils.isEmpty(empInfoDOList)) {
            EmpInfoDO empInfoDO = null;
            //找到与登录租户相同的员工
            for (EmpInfoDO empInfo : empInfoDOList) {
                if (empInfo.getSiteId().equals(userLoginVO.getSiteId())) {

                    empInfoDO = empInfo;
                }
            }
            // 当前登录用户的租户下没有员工，则切换到有员工的租户下
            if (Objects.isNull(empInfoDO)) {
                empInfoDO = empInfoDOList.get(0);
                log.info("站点下不存在员工，切换租户到：" + empInfoDO.getSiteId());
                // 切换到当前员工用户的租户下，即登录到另一个站点
                JwtTokenDTO jwtTokenDTO = userProviderService.switchToken(userLoginVO.getJwtTokenDTO().getToken(), empInfoDO.getSiteId());
                userLoginVO.setJwtTokenDTO(jwtTokenDTO);
            }
            log.info("员工所在公司:" + empInfoDO.getCompanyId());
            CompanyDO companyDO = companyDAO.selectByPrimaryKey(empInfoDO.getCompanyId());
            if (empInfoDO.getUnionId() == null) {
                empInfoDO.setUnionId(userLoginVO.getUnionId());
                empInfoService.updateByIdSelective(empInfoDO, empInfoDO.getSiteId(), empInfoDO.getCreateUserId());
            }
            EmpAllInfoVO empAllInfoVO = empInfoDO.to(EmpAllInfoVO.class);
            empAllInfoVO.setCompanyName(companyDO.getName());
            empLoginResultVO.setEmpAllInfoVO(empAllInfoVO);
            //更新emp_info头像
            if (StringUtils.isNotBlank(empLoginResultVO.getHeadImgUrl()) && empInfoDO != null) {
                EmpInfoDO updatePhote = new EmpInfoDO();
                updatePhote.setId(empInfoDO.getId());
                updatePhote.setEmpPhoto(empLoginResultVO.getHeadImgUrl());
                empInfoService.updateByIdSelective(updatePhote);
            }
        }
        return empLoginResultVO;
    }
    
     @Override
    public EmpLoginResultVO verificationCodeLoginWeChatForAdmin(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        EmpLoginResultVO empLoginResultVO = new EmpLoginResultVO();
        if (StringUtils.isBlank(phoneVerificationCodeDTO.getPhone())
                || StringUtils.isBlank(phoneVerificationCodeDTO.getCode())) {
            throw new ParamException("手机号和验证码不可为空");
        }
        //校验短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phoneVerificationCodeDTO.getPhone());
        shortMessageDTO.setScene(USER_CERTIFICATION);
        shortMessageDTO.setCaptcha(phoneVerificationCodeDTO.getCode());
        shortMessageDTO.setAppName(appName);
        TokenUser tokenUser = iCertificationService.selectByPhoneAndDomain(phoneVerificationCodeDTO.getPhone(),
                phoneVerificationCodeDTO.getHost());
        boolean check = sendShortMessage.verifyCaptcha(shortMessageDTO, tokenUser);
//        boolean check = messageProviderService.verifyCaptcha("hr_emp_certification"
//                , phoneVerificationCodeDTO.getPhone(), phoneVerificationCodeDTO.getCode());
        if (!check) {
            throw new ParamException("验证码错误");
        }
        // 根据unionId获取第三方信息
        ThirdInfoDO thirdInfo = userProviderService.selectByUnionId(phoneVerificationCodeDTO.getUnionId());
        log.info("绑定手机号验证码thirdInfoDOTmp：{}", thirdInfo);
        if (Objects.nonNull(thirdInfo)) {
            empLoginResultVO.setHeadImgUrl(thirdInfo.getHeadImgUrl());
            empLoginResultVO.setNickName(thirdInfo.getNickName());
        }

        UserDO userDO = userProviderService.selectByPhone(phoneVerificationCodeDTO.getPhone());
        ParamException.notNull(userDO, "所填手机号用户不存在");
        //绑定微信并登录
        UserLoginVO userLoginVO = userProviderService.bindWechatAndLogin(userDO.getId()
                , UserConstants.TO_B, phoneVerificationCodeDTO.getUnionId());
        empLoginResultVO.setUserLoginVO(userLoginVO);
        return empLoginResultVO;
    }

    @Override
    public String verificationCodeLoginWeChatByDevicetypeNoLogic(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        if (StringUtils.isBlank(phoneVerificationCodeDTO.getPhone()) || StringUtils.isBlank(phoneVerificationCodeDTO.getCode())) {
            throw new ParamException("手机号和验证码不可为空");
        }
        //校验短信验证码
        ShortMessageDTO shortMessageDTO = new ShortMessageDTO();
        shortMessageDTO.setPhone(phoneVerificationCodeDTO.getPhone());
        shortMessageDTO.setScene(USER_CERTIFICATION);
        shortMessageDTO.setCaptcha(phoneVerificationCodeDTO.getCode());
        shortMessageDTO.setAppName(appName);
        TokenUser tokenUser = iCertificationService.selectByPhoneAndDeviceType(phoneVerificationCodeDTO.getPhone(),
                phoneVerificationCodeDTO.getDeviceType());
        boolean check = sendShortMessage.verifyCaptcha(shortMessageDTO,tokenUser);
        if (!check) {
            throw new ParamException("验证码错误");
        } else {
            return "success";
        }
    }

    @Override
    public Boolean hrVerificationCodeOfficialAccount(PhoneVerificationCodeDTO phoneVerificationCodeDTO) {
        if (StringUtils.isBlank(phoneVerificationCodeDTO.getPhone()) || StringUtils.isBlank(phoneVerificationCodeDTO.getCode())) {
            throw new ParamException("手机号和验证码不可为空");
        }

        //校验短信验证码
        boolean check = messageProviderService.verifyCaptcha("hr_emp_certification", phoneVerificationCodeDTO.getPhone(), phoneVerificationCodeDTO.getCode());
        if (!check) {
            throw new ParamException("验证码错误");
        }

        return true;
    }

    /**
     * 根据手机号查询待入职员工
     *
     * @param phone 手机号
     */
    private EmpInfoDO checkEmpInfo(String phone) {
        //根据手机号查询员工是否存在
        Example example = new Example(EmpInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
//                .andEqualTo("status", 1)
                .andEqualTo("phone", phone)
                .andNotEqualTo("status", 4);
        return empInfoDAO.selectOneByExample(example);
    }

    /**
     * 更新员工userId
     *
     * @param empInfoDO 员工DO
     */
    private void updateEmpUserId(EmpInfoDO empInfoDO) {
        //更新员工userId
        empInfoDO.setGmtModified(new Date());
        empInfoDO.setModifiedUserId(empInfoDO.getCreateUserId());
        empInfoDAO.updateByPrimaryKey(empInfoDO);
    }

    @Override
    public EmpInfoDO checkEmpUnionIdBind(String unionId) {
        Example example = new Example(EmpInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andIn("status", Arrays.asList(2, 3))
                .andEqualTo("unionId", unionId);
        List<EmpInfoDO> empInfoDOList = empInfoDAO.selectByExample(example);
        return CollectionUtils.isEmpty(empInfoDOList) ? null : empInfoDOList.get(0);
    }

    @Override
    public Boolean checkPhoneBind(WeChatBindFromOffAccountDTO weChatBindFromOffAccountDTO) {
        if (StringUtils.isBlank(weChatBindFromOffAccountDTO.getPhone())) {
            throw new ParamException("手机号不可为空");
        }
        if (StringUtils.isBlank(weChatBindFromOffAccountDTO.getUnionId())) {
            throw new ParamException("unionId不可为空");
        }
        // 根据租户ID和手机号查询员工
        Example example = new Example(EmpInfoDO.class);
        example.and()
                .andEqualTo("phone", weChatBindFromOffAccountDTO.getPhone())
//                .andEqualTo("siteId", weChatBindFromOffAccountDTO.getSiteId())
                .andNotEqualTo("status", 4)
                .andEqualTo("valid", true);
        EmpInfoDO empInfoDO = empInfoDAO.selectOneByExample(example);
        if (!Objects.nonNull(empInfoDO)) {
            throw new ParamException("该手机号没有员工信息，请重新输入手机号");
        } else if (StringUtils.isNotBlank(empInfoDO.getUnionId())) {
            if (!weChatBindFromOffAccountDTO.getUnionId().equals(empInfoDO.getUnionId())) {
                throw new ParamException("该手机号已绑定其他微信");
            }
            throw new ParamException("该手机号已绑定过，无需重新绑定");
        }
        return true;
    }

}
